package cc.liyinghao.controller;

import cc.liyinghao.common.api.Result;
import cc.liyinghao.response.AddUserListVo;
import cc.liyinghao.response.ContactsListVo;
import cc.liyinghao.service.ContactsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * 用户关系管理
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/friendship")
@Api(tags = "用户联系人管理")
public class ContactsController {

    private final ContactsService contactsService;

    @PostMapping("/add")
    @ApiOperation(value = "添加联系人")
    public Result<String> addFriendship(@RequestParam int id)  {
        int newId = contactsService.addFriendship(id);
        if (Objects.equals(newId, 0)) {
            return Result.error("添加失败");
        }
        return Result.success("添加成功");
    }

    @GetMapping("/get_new_add_list_other") // 我发给别人的(已发送的邀请)
    @ApiOperation(value = "(已发送的邀请)用户向别人新添加联系人列表")
    public Result<List<AddUserListVo>> getToOtherNewAddList() {
        return Result.success(contactsService.getNewAddList());
    }

    @GetMapping("/get_new_add_list")
    @ApiOperation(value = "(新联系人)别人向用户新添加联系人列表") // 别人发给我的(新联系人)
    public Result<List<AddUserListVo>> getToSelfNewAddList() {
        return Result.success(contactsService.getNewAddListOther());
    }

    @PostMapping("/sure_add")
    @ApiOperation(value = "确认添加联系人")
    public Result<String> sureAddFriendship(@RequestParam int id) {
        int newId = contactsService.sureAddFriendship(id);
        log.info("确认添加好友，id:{}", newId);
        if (Objects.equals(newId, 0)) {
            return Result.error("确认添加失败");
        }
        return Result.success("确认添加成功");
    }

    @PostMapping("/reject_add")
    @ApiOperation(value = "拒绝添加联系人")
    public Result<String> rejectedContacts(@RequestParam int id) {
        int newId = contactsService.sureAddFriendship(id);
        log.info("拒绝添加好友，id:{}", newId);
        if (Objects.equals(newId, 0)) {
            return Result.error("确认添加失败");
        }
        return Result.success("确认添加成功");
    }

    @GetMapping("/get_my_contacts")
    @ApiOperation(value = "获取我的联系人列表")
    public Result<List<ContactsListVo>> getMyContacts() {
        return Result.success(contactsService.getContactsList());
    }
}
